package com.crm.web.controller;

import com.crm.entity.Svr;
import com.crm.entity.User;
import com.crm.service.CustomerService;
import com.crm.service.DictService;
import com.crm.service.IUserService;
import com.crm.service.SvrService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/service")
public class SvrController {

    @Resource
    DictService dictService;
    @Resource
    SvrService svrService;
    @Resource
    CustomerService customerService;
    @Resource
    IUserService userService;

    @RequestMapping("/dispatch")
    public String dispatch(Model model,
                           @RequestParam(required = false, defaultValue = "1") Integer pageIndex,
                           @RequestParam(required = false, defaultValue = "") String svrCustName,
                           @RequestParam(required = false, defaultValue = "") String svrType,
                           @RequestParam(required = false, defaultValue = "") String svrTitle) {
        Sort sort = new Sort(Sort.Direction.ASC, "svrId");
        Pageable pageable = PageRequest.of(pageIndex - 1, 3, sort);
        Page<Svr> svrPage = svrService.findAll(svrCustName,
                svrTitle, svrType, pageable);
        model.addAttribute("users", userService.findAll());
        model.addAttribute("servicePage", svrPage);
        model.addAttribute("svrCustName", svrCustName);
        model.addAttribute("svrType", svrType);
        model.addAttribute("svrTitle", svrTitle);
        model.addAttribute("dicts", dictService.findSvrType());
        return "service/dispatch";
    }

    @RequestMapping("/add")
    public String toAdd(Model model) {
        model.addAttribute("customers", customerService.findAll());
        model.addAttribute("dicts", dictService.findSvrType());
        return "service/add";
    }

    @RequestMapping("/save")
    public String save(Svr svr) {
        try {
            svrService.save(svr);
            return "redirect:/service/dispatch";
        } catch (Exception e) {
            return "service/add";
        }
    }

    /**
     * 分配
     *
     * @return
     */
    @RequestMapping("/allot/{svrId}/{svrDueId}")
    @ResponseBody
    public Map allot(@PathVariable Integer svrId, @PathVariable Integer svrDueId) {
        Svr svr = svrService.getServiceBySvrId(svrId);
        User user = userService.getUser(svrDueId);
        svr.setSvrDueId(user.getUsrId());
        svr.setSvrDueTo(user.getUsrName());
        svr.setSvrStatus("已分配");
        svrService.save(svr);
        Map map = new HashMap();
        map.put("editResult", "true");
        return map;
    }

    @RequestMapping("/del/{svrId}")
    @ResponseBody
    public Map del(@PathVariable Integer svrId) {
        Map map = new HashMap();
        svrService.del(svrId);
        map.put("delResult", "true");
        return map;
    }

    @RequestMapping("/deal")
    public String deal(Model model,
                       @RequestParam(required = false, defaultValue = "1") Integer pageIndex,
                       @RequestParam(required = false, defaultValue = "") String svrCustName,
                       @RequestParam(required = false, defaultValue = "") String svrType,
                       @RequestParam(required = false, defaultValue = "") String svrTitle) {
        Sort sort = new Sort(Sort.Direction.ASC, "svrId");
        Pageable pageable = PageRequest.of(pageIndex - 1, 3, sort);
        Page<Svr> svrPage = svrService.findAll(svrCustName,
                svrTitle, svrType, pageable);
        model.addAttribute("page", svrPage);
        model.addAttribute("svrCustName", svrCustName);
        model.addAttribute("svrType", svrType);
        model.addAttribute("svrTitle", svrTitle);
        model.addAttribute("dicts", dictService.findSvrType());
        return "service/deal";
    }

    @RequestMapping("/edit")
    public String dealEdit(Integer svrId, Model model) {
        model.addAttribute("serviced", svrService.getServiceBySvrId(svrId));
        return "service/dealEdit";
    }

    @RequestMapping("/editDeal")
    public String editDeal(Svr svr) {
        try {
            svrService.save(svr);
            return "redirect:/service/deal";
        } catch (Exception e) {
            return "service/dealEdit";
        }
    }

    @RequestMapping("/feedback")
    public String toFeedBack(Model model,
                             @RequestParam(required = false, defaultValue = "1") Integer pageIndex,
                             @RequestParam(required = false, defaultValue = "") String svrCustName,
                             @RequestParam(required = false, defaultValue = "") String svrType,
                             @RequestParam(required = false, defaultValue = "") String svrTitle) {
        Sort sort = new Sort(Sort.Direction.ASC, "svrId");
        Pageable pageable = PageRequest.of(pageIndex - 1, 3, sort);
        Page<Svr> svrPage = svrService.findAll(svrCustName,
                svrTitle, svrType, pageable);
        model.addAttribute("feedpage", svrPage);
        model.addAttribute("dicts", dictService.findSvrType());
        model.addAttribute("svrCustName", svrCustName);
        model.addAttribute("svrType", svrType);
        model.addAttribute("svrTitle", svrTitle);
        return "service/feedback";
    }


    @RequestMapping("/toFeedBack")
    public String toFeedBack(Integer svrId, Model model) {
        model.addAttribute("serviced", svrService.getServiceBySvrId(svrId));
        model.addAttribute("dicts", dictService.findSvrType());
        return "service/feedBackEdit";
    }

    @RequestMapping("/editFB")
    public String editFB(Svr svr) {
        try {
            svrService.save(svr);
            return "redirect:/service/feedback";
        } catch (Exception e) {
            return "service/feedBackEdit";
        }
    }

    @RequestMapping("/arch")
    public String arch(Model model,
                       @RequestParam(required = false, defaultValue = "1") Integer pageIndex,
                       @RequestParam(required = false, defaultValue = "") String svrCustName,
                       @RequestParam(required = false, defaultValue = "") String svrType,
                       @RequestParam(required = false, defaultValue = "") String svrTitle) {
        Sort sort = new Sort(Sort.Direction.ASC, "svrId");
        Pageable pageable = PageRequest.of(pageIndex - 1, 3, sort);
        Page<Svr> svrPage = svrService.findByIdArchived(svrCustName,
                svrTitle, svrType, pageable);
        model.addAttribute("servicePage", svrPage);
        model.addAttribute("dicts", dictService.findSvrType());
        model.addAttribute("svrCustName", svrCustName);
        model.addAttribute("svrType", svrType);
        model.addAttribute("svrTitle", svrTitle);
        return "service/arch";
    }

    @RequestMapping("findArch")
    public String findArch(Integer svrId, Model model){
        model.addAttribute("serviced", svrService.getServiceBySvrId(svrId));
        return "service/archInfo";
    }

}
